/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.controller;

import com.tamnhat.entity.ContTemDetail;
import com.tamnhat.entity.ContentTemplate;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class ContTemController implements Serializable {
    
    private String id;
    private String name;
    private String status;
    private ContentTemplate current;
    private ContentTemplate oldCurrent;
    private List<ContentTemplate> listCT = new ArrayList<ContentTemplate>();
    private DataModel list;
    private String action = "free";
    private String nameS = "";
    private String nameDel = "";
    
    /**
     * Creates a new instance of ContTemController
     */
    public ContTemController() {
        try {
            listCT = current.allCT("");
            list = new ListDataModel(listCT);
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public ContentTemplate getOldCurrent() {
        return oldCurrent;
    }

    public void setOldCurrent(ContentTemplate oldCurrent) {
        this.oldCurrent = oldCurrent;
    }

    public List<ContentTemplate> getListCT() {
        return listCT;
    }

    public void setListCT(List<ContentTemplate> listCT) {
        this.listCT = listCT;
    }

    public String getNameS() {
        return nameS;
    }

    public void setNameS(String nameS) {
        this.nameS = nameS;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public ContentTemplate getCurrent() {
        return current;
    }

    public void setCurrent(ContentTemplate current) {
        this.current = current;
    }

    public DataModel getList() {
        return list;
    }

    public void setList(DataModel list) {
        this.list = list;
    }

    public String getNameDel() {
        return nameDel;
    }

    public void setNameDel(String nameDel) {
        this.nameDel = nameDel;
    }

    //-----------------------------------------

    public boolean checkInput() {
        if (name == null || name.equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Tên template không được bỏ trống!"));
            return false;
        }
        return true;
    }

    public String search() {
        try {
            listCT = current.allCT(nameS);
            list = new ListDataModel(listCT);
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public String contTFromTable() {
        current = (ContentTemplate) list.getRowData();
        oldCurrent = current;
        id = current.getId();
        name = current.getName();
        status = current.getStatus();
        return "";
    }

    public String contDelFromTable() {
        current = (ContentTemplate) list.getRowData();        
        return "";
    }

    public String update() {
        try {
            if (checkInput()) {                                
                current = new ContentTemplate(id, name, status);
                if (current.update()) {
                    listCT = current.allCT(nameS);
                    list = new ListDataModel(listCT);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Câp nhật bản ghi thành công!"));
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public String updateInsert() {
        current = new ContentTemplate();
        return "";
    }

    public String insert() {
        try {
            if (checkInput()) {
                current = new ContentTemplate(id, name, status);
                if (current.insert()) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Thêm bản ghi thành công!"));
                    listCT = current.allCT(nameS);
                    list = new ListDataModel(listCT);
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public String remove() {
        try {
            current = (ContentTemplate) list.getRowData();
            if (current.remove()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công!"));
                listCT.remove(current);
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }
}
